<style lang="less">
  .card_style {
    height: 100%;
    width: 100%;
    overflow: hidden;
    .ivu-card-body {
      padding: 0px;
    }
  }
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .search-info-style {
    margin: 10px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
  .modal-form-input {
    width: 80%;
    margin-left: 50px;
  }
</style>
<template>
  <Card class="card_style" :bordered="false" :dis-hover="true">
    <Row class="search-info-style">
      <Col span="6">
        <Select v-model="searchInfo.system" style="width: 90%">
          <Option value="">全部</Option>
          <Option value="CJRBT">残疾人双补</Option>
        </Select>
      </Col>
      <Button class="new-btn-sty" @click="init">
        <Icon type="ios-search" /> 搜索
      </Button>
      <Button class="new-btn-sty" @click="exportData">
        <Icon type="ios-download-outline" /> 导出数据
      </Button>
    </Row>
    <Table border :columns="columns1" :data="datalist" ref="table1" show-summary :summary-method="handleSummary">
      <template slot-scope="{ row, index }" slot="submit_status">
        <span>{{dictData['a' + row.submit_status]}}</span>
      </template>
      <template slot-scope="{ row, index }" slot="actual_money">
        <span v-if="row.submit_status=='5'">{{row.actual_money}}</span>
        <span v-else>-</span>
      </template>
      <template slot-scope="{ row, index }" slot="table-btns">
        <!-- <span style="margin-right: 5px">
          <Button type="info" size="small" @click="editInfo(row)">查看</Button>
        </span> -->
      </template>
    </Table>
    <Row v-if="loadData">
      <Col class="demo-spin-col" span="8" offset="8">
        <Spin fix>
          <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
          <div>数据正在加载</div>
        </Spin>
      </Col>
    </Row>

  </Card>
</template>
<script>
import { getStatisticsList } from '@/api/capital'
export default {
  components: {},
  data () {
    return {
      loadData: true,
      searchInfo: {
        system: ''
      },
      dictData: {
        'a2': '审核中',
        'a20': '区县审核不通过',
        'a3': '区县审核通过',
        'a30': '市级审核不通过',
        'a4': '市级审核通过',
        'a40': '市财政审核不通过',
        'a5': '审核通过'
      },
      columns1: [
        { title: '序号', type: 'index', key: 'index', width: 100, align: 'center' },
        { title: '审核阶段', slot: 'submit_status', key: 'submit_status1' },
        { title: '应发金额(元)', key: 'submit_money', width: 240, align: 'center' },
        { title: '实发金额(元)', slot: 'actual_money', key: 'actual_money', width: 240, align: 'center' },
        { title: '操作', align: 'center', slot: 'table-btns', width: 200 }
      ],
      datalist: []
    }
  },
  methods: {
    exportData () {
      if (this.datalist.length === 0) {
        this.$Notice.warning({ title: '导出失败', desc: '无统计数据' })
        return
      }
      let arr = []
      let total = { submit_status1: '合计', submit_money: 0, actual_money: 0 }
      this.datalist.forEach((column, index) => {
        arr.push({
          submit_status1: this.dictData['a' + column.submit_status],
          submit_money: column.submit_money,
          actual_money: column.actual_money
        })
        total.submit_money += column.submit_money
        total.actual_money += column.actual_money
      })
      arr.push(total)
      this.$refs.table1.exportCsv({
        filename: '信息统计',
        columns: this.columns1.slice(1, 4),
        data: arr,
        original: false
      })
    },
    handleSummary ({ columns, data }) {
      const sums = {}
      columns.forEach((column, index) => {
        const key = column.key
        if (index === 0) {
          sums[key] = { key, value: '合计' }
          return
        }
        const values = data.map(item => Number(item[key]))
        if (!values.every(value => isNaN(value))) {
          const v = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[key] = { key, value: v }
        } else {
          sums[key] = { key, value: '' }
        }
      })
      return sums
    },
    // 查询订单列表接口
    init () {
      getStatisticsList(this.searchInfo).then(res => {
        if (res.data.data) {
          this.datalist = res.data.data
          this.loadData = false
        } else {
          this.loadData = true
          this.$Notice.error({ title: '统计失败', desc: res.data.errmsg })
        }
      })
    }
  },
  mounted () {
    this.init()
  }
}
</script>
